/*
 * Copyright (c) 2005 - 2007 metadude.com, All rights reserved.
 * Email: community@polarion.org
 *
 * This program and the accompanying materials are made available under the
 * terms of the GNU General Public License 2.0 (the "License"). You may not use
 * this file except in compliance with the License. Copy of the License is
 * located in the file LICENSE.txt in the project distribution. You may also
 * obtain a copy of the License at http://www.gnu.org/licenses/gpl.html
 *
 *
 * METADUDE.COM MAKES NO REPRESENTATIONS OR WARRANTIES
 * ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESSED OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. POLARION SOFTWARE
 * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT
 * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 */
using System.Diagnostics;
using System.Threading;
using mshtml;
using poba.Core.Html;
using SHDocVw;

namespace poba.Core.Drivers {
    /// <summary>
    /// How to manipulate frames.
    /// http://www.codeguru.com/cpp/i-n/internet/browsercontrol/article.php/c13065/
    /// </summary>
    public class IEDriverBase {
        private bool _isDocumentComplete = false;
        private InternetExplorer _internetExplorer;
        private Process _internetExplorerProcess;
        private int _timeoutSeconds = 30;

        public InternetExplorer InternetExplorer {
            get { return _internetExplorer; }
        }

        public int TimeoutSeconds {
            get { return _timeoutSeconds; }
            set { _timeoutSeconds = value; }
        }

        public IEDriverBase() {
            _internetExplorerProcess = Process.Start("IExplore.exe");

            Thread.Sleep(500);
            ShellWindows shellWindows = new ShellWindowsClass();
            foreach (InternetExplorer Browser in shellWindows) {
                if (Browser.HWND == (int)_internetExplorerProcess.MainWindowHandle) {
                    _internetExplorer = Browser;
                    break;
                }
            }

            _internetExplorer.Visible = true;
            _internetExplorer.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(IE_DocumentComplete);
        }

        public void Close() {
            _internetExplorerProcess.Kill();
//            _internetExplorerProcess.Close();
        }

        public Element GetElementById(string elementId) {
            HTMLDocument document = ((HTMLDocument)InternetExplorer.Document);
            IHTMLElement element = document.getElementById(elementId);

            int nullElementCount = 0;
            // The following loop is to account for any latency that EXPLORER
            // might experience.  Tweak the number of times to attempt
            // to continue checking the document before giving up.
            while (element == null && nullElementCount < 10) {
                Thread.Sleep(500);
                element = document.getElementById(elementId);
                nullElementCount++;
            }


            return Element.CreateMostSpecificType(element);
        }

        public void Navigate(string url) {
            _isDocumentComplete = false;
            object empty = "";
            InternetExplorer.Navigate(url, ref empty, ref empty, ref empty, ref empty);
            WaitForComplete();
        }

        private void IE_DocumentComplete(object pDisp, ref object URL) {
            _isDocumentComplete = true;
        }

        private void WaitForComplete() {
            int elapsedSeconds = 0;
            while (!_isDocumentComplete && elapsedSeconds != TimeoutSeconds) {
                Thread.Sleep(1000);
                elapsedSeconds++;
            }
        }
    }
}